package service

import (
	"flare-admin/service/dto/ztrust"

	"github.com/gin-gonic/gin"
	"github.com/jackc/pgx/v5/pgtype"
	"github.com/sirupsen/logrus"
)

func (s *Service) GetAllZtrustEvents(ctx *gin.Context) ([]ztrust.ZtrustEventDTO, error) {
	events, err := s.dao.Ztrust.SelectAllEvents(ctx)
	if err != nil {
		logrus.Errorf("[service] - [GetAllZtrustEvents] - failed to get all ZTrust events: %v", err)
		return nil, err
	}
	return ztrust.ZtrustEventDao2DTOs(events), nil
}

// 获取某个 Cybertwin 的信任事件
func (s *Service) GetCtZtrustEvents(ctx *gin.Context, cybertwinID string) ([]ztrust.ZtrustEventDTO, error) {
	var ctid pgtype.Numeric
	err := ctid.Scan(cybertwinID)
	if err != nil {
		logrus.Errorf("[service] - [GetCtZtrustEvents] - failed to scan cybertwinID: %v", err)
		return nil, err
	}

	events, err := s.dao.Ztrust.SelectCtEvents(ctx, ctid)
	if err != nil {
		logrus.Errorf("[service] - [GetCtZtrustEvents] - failed to get cybertwin ZTrust events: %v", err)
		return nil, err
	}
	return ztrust.ZtrustEventDao2DTOs(events), nil
}

func (s *Service) GetAllZtrustScores(ctx *gin.Context) ([]ztrust.ZtrustScoreDTO, error) {
	scores, err := s.dao.Ztrust.SelectAllScores(ctx)
	if err != nil {
		logrus.Errorf("[service] - [GetAllZtrustScores] - failed to get all ZTrust scores: %v", err)
		return nil, err
	}
	return ztrust.ZtrustScoreDao2DTOs(scores), nil
}

func (s *Service) GetAllZtrustAccessEvents(ctx *gin.Context) ([]ztrust.ZtrustAccessEventDTO, error) {
	records, err := s.dao.Ztrust.SelectAccessEvent(ctx)
	if err != nil {
		logrus.Errorf("[service] - [GetAllZtrustAccessEvents] - failed to get all ZTrust access events: %v", err)
		return nil, err
	}
	return ztrust.ZtrustAccessEventDAO2DTOs(records), nil
}
